<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" @row-click="handleRowClick" border :height="autoHeight">
      <el-table-column prop="paramLabel" label="参数名称" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <el-input v-if="scope.row.editAbled" v-model="scope.row[scope.column.property]" />
          <span v-else>{{ scope.row[scope.column.property] }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="paramMapper" label="参数属性" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <el-input v-if="scope.row.editAbled" v-model="scope.row[scope.column.property]" />
          <span v-else>{{ scope.row[scope.column.property] }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="paramLocation" label="参数位置" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-select v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" style="width: 100%" clearable>
            <el-option v-for="item in locations" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </template>
        <template slot-scope="scope">
          <el-select :disabled="!scope.row.editAbled" v-model="scope.row[scope.column.property]" :placeholder="scope.column.label" style="width: 100%" clearable>
            <el-option v-for="item in locations" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="dynamicProp" label="动态属性" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <el-input v-if="scope.row.editAbled" v-model="scope.row[scope.column.property]" />
          <span v-else>{{ scope.row[scope.column.property] }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="defValue" label="默认值" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
        <template slot-scope="scope">
          <el-input v-if="scope.row.editAbled" v-model="scope.row[scope.column.property]" />
          <span v-else>{{ scope.row[scope.column.property] }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="valueType" label="参数类型" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-select v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" style="width: 100%" clearable>
            <el-option v-for="item in valueTypes" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </template>
        <template slot-scope="scope">
          <el-select :disabled="!scope.row.editAbled" v-model="scope.row[scope.column.property]" :placeholder="scope.column.label" style="width: 100%" clearable>
            <el-option v-for="item in valueTypes" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="mustNeed" label="必传" align="center">
        <template slot-scope="scope">
          <el-switch :disabled="!scope.row.editAbled" v-model="scope.row[scope.column.property]" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120px">
        <template slot="header">
          <el-button type="primary" @click="() => { dataSource.splice(0,0,{editAbled:true, requestId: requestId })}" icon="el-icon-circle-plus-outline"></el-button>
        </template>
        <div slot-scope="scope">
          <el-button v-if="scope.row.editAbled" type="warning" icon="el-icon-circle-check" circle @click="saveData(scope.row)" title="保存"></el-button>
          <el-button v-else type="success" icon="el-icon-edit" circle @click="() => { $set(scope.row, 'editAbled', true)}" title="编辑"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "RequestParam",
  mixins: [IdeaTable],
  props: {
    requestId: {
      type: String,
      default: '0'
    }
  },
  created() {
    this.queryUrl = '/requestParam/queryPage'
    this.saveUrl = '/requestParam/save'
    this.removeUrl = '/requestParam/delete/'
    this.rowKey = 'paramId'
    this.addFilter({ field: 'requestId', op: '=', value: this.requestId })
    this.pageInfo.sorts.push({ field: 'updateAt', asc: false })
    this.loadSource()
  },
  data() {
    return {
      locations: [
        { value: 'path', label: '请求路径' },
        { value: 'query', label: '查询参数' },
        { value: 'header', label: '请求头' },
        { value: 'cookie', label: 'cookie' },
        { value: 'body', label: '请求体' },
      ],
      valueTypes: [
        { value: 'String', label: '字符串' },
        { value: 'Number', label: '数字' },
        { value: 'File', label: '文件' },
        { value: 'Object', label: '对象' },
        { value: 'Array', label: '数组' },
      ]
    }
  },
  computed: {
    autoHeight() {
      return `calc(100vh - 370px)`
    }
  },
  methods: {
    initEntity(){
      this.entityInfo = { dataState: 0 }
    }
  }
}
</script>

<style scoped>

</style>
